package zhklappoint.initsys;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;

import zhklappoint.entity.FunctionKinds;
import zhklappoint.enums.ENUM_FUNCTION_IMG;
import zhklappoint.enums.ENUM_FUNCTION_STATUS;
import zhklappoint.enums.ENUM_ISDELETE;
import zhklappoint.service.FunctionKindsService;


@Component
@Order(value = 1)
public class ApplicationInitSystem implements ApplicationRunner {
	
	
	@Autowired
	private FunctionKindsService fks;
	
	
	@Override
	public void run(ApplicationArguments args) throws Exception {
		FunctionKinds fkc = new FunctionKinds();
		Page<FunctionKinds> pfks = fks.getFunctionKindsList(fkc, 1, 100);
		List<Integer> codeList= new ArrayList<Integer>();
		for(ENUM_FUNCTION_IMG e:ENUM_FUNCTION_IMG.values()) {
			Integer i=Integer.valueOf(e.name().replace("IMG", ""));
//			System.out.println("i:"+i);
			codeList.add(i);
		}
			for(Integer i:codeList) {//枚举里面的数据逼数据库的多或者等同
					Boolean b= true;
					for(FunctionKinds f:pfks.getContent()) {//当前标志存在只需要更新信息
						if(f.getCode()==i) {
							f.setIcon(ENUM_FUNCTION_IMG.getIcon(i));
							f.setName(ENUM_FUNCTION_IMG.getName(i));
							f.setStatus(ENUM_FUNCTION_IMG.isPay(i)?ENUM_FUNCTION_STATUS.PAID.value:ENUM_FUNCTION_STATUS.FREE.value);
							fks.saveFunctionKinds(f);
							b=false;
						}
					}
					if(b) {//当前信息不存在  新建一个
						FunctionKinds fk = new FunctionKinds();
						fk.setIcon(ENUM_FUNCTION_IMG.getIcon(i));
						fk.setCode(i);
						fk.setName(ENUM_FUNCTION_IMG.getName(i));
						fk.setStatus(ENUM_FUNCTION_IMG.isPay(i)?ENUM_FUNCTION_STATUS.PAID.value:ENUM_FUNCTION_STATUS.FREE.value);
						fks.saveFunctionKinds(fk);
					}
				}
		for(FunctionKinds c:pfks.getContent()) {//枚举里面的数据逼数据库的少
			Boolean isexist=false;
			for(Integer ce:codeList) {
				if(ce==c.getCode()) {
					isexist=true;
					break;
				}
			}
			if(!isexist) {
				c.setIsdelete(ENUM_ISDELETE.DELETED.value);
				fks.saveFunctionKinds(c);
			}
		}
		System.out.println("-----------   --------------------------------");
		System.out.println(" **知***桓****预*****约******欢迎您！~****");
		System.out.println("-----------   --------------------------------");
	}
}
